package tuan.utils;

public class StringUtil {
	public static String getPureText(String text) {
		if (text == null) {
			return "";
		}
		text = text.replaceAll("[ \\s]*", "");
		return text.replaceAll("&[^;];", "");
	}

	public static String getUrlCurentPath(String url) {
		if (url.indexOf("?") >= 0) {
			url = url.substring(0, url.indexOf("?"));
		}
		if (url.lastIndexOf("/") >= 0) {
			url = url.substring(0, url.lastIndexOf("/"));
		}
		return url;
	}

	public static String getFullUrl(String url, String path) {
		path = path.trim();
		url = url.trim();
		if (path.startsWith("http://")) {
			return path;
		}
		if (path.startsWith("/")) {
			return getDomain(url) + path;
		}
		if (path.startsWith("./")) {
			path = path.substring(2);
		}
		return getPath(url) + "/" + path;

	}

	public static String getDomain(String url) {
		if (url.startsWith("http://")) {
			url = url.substring(7);
		}
		if (url.indexOf("/") >= 0) {
			return "http://" + url.substring(0, url.indexOf("/"));
		}
		return "http://" + url;
	}

	public static String getPath(String url) {
		if (url.startsWith("http://")) {
			url = url.substring(7);
		}
		if (url.indexOf("?") >= 0) {
			url = url.substring(0, url.indexOf("?"));
		}
		if (url.lastIndexOf("/") >= 0) {
			url = url.substring(0, url.lastIndexOf("/"));
		}
		return "http://" + url;
	}
}
